<template>
	<view class="container">
		<view class="list-cell" hover-class="cell-hover" :hover-stay-time="50">
			<text class="cell-tip full">
				<input type="text" :value="nickname" @input="syncData" @blur="syncData" />
			</text>
		</view>
		<view class="action">
			<button @click="submit" type="primary">提交</button>
		</view>
	</view>
</template>

<script>
	import { mapState, mapMutations } from 'vuex';
	export default {
		components: {
		},
		computed: {
			...mapState(['app', 'user'])
		},
		data() {
			return {
				nickname: ''
			}
		},
		onLoad(options){
			this.nickname = this.user.userInfo.nickname;
		},
		methods: {
			syncData(e) {
				this.nickname = e.detail.value;
			},
			async submit(){
				let res = await this.util.request({
					url: '/v1/core/user/nickname',
					data: {
						nickname: this.nickname
					},
					method: 'put',
				});
				if (res.code == 200) {
					let userInfo = this.user.userInfo;
					userInfo.nickname = this.nickname;
					this.$store.commit('user/setUserInfo', userInfo);
					this.util.showToast({
						title: res.msg,
						icon: 'success',
						duration: 1000
					});
					uni.navigateBack();
				} else {
					this.util.showToast({
						title: res.msg,
						icon: 'loading',
						duration: 2000
					});
				}
			}
		}
	}
</script>

<style lang='scss'>
	page {
		background: $page-color-base;
	}
	.action {
		padding: 20px;
		button[type="primary"] {
			background-color: $uni-color-primary;
		}
	}
	.list-cell {
		display: flex;
		align-items: center;
		padding: 20upx $page-row-spacing;
		line-height:60upx;
		position:relative;
		background: #fff;
		justify-content: center;
		&.log-out-btn {
			margin-top: 40upx;
			.cell-tit{
				color: $uni-color-primary;
				text-align: center;
				margin-right: 0;
			}
		}
		&.cell-hover {
			background:#fafafa;
		}
		&.b-b:after {
			left: 30upx;
		}
		&.m-t {
			margin-top: 16upx; 
		}
		.cell-more {
			align-self: center;
			font-size:$font-lg;
			color:$font-color-light;
			margin-left:10upx;
		}
		.cell-tit {
			flex: 1;
			font-size: $font-base + 2upx;
			color: $font-color-dark;
			margin-right:10upx;
		}
		.cell-tip {
			font-size: $font-base;
			color: $font-color-light;
			image {
				max-width: 120upx;
				max-height: 120upx;
			}
			&.full {
				width: 100%;
			}
		}
		switch {
			transform: translateX(16upx) scale(.84);
		}
	}
</style>
